package com.blast.comicsisland.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.blast.comicsisland.R;
import com.blast.comicsisland.adapters.ChapterAdapter;
import com.blast.comicsisland.model.commic.commiccatalog.BookPart;
import com.blast.comicsisland.model.commic.commiccatalog.Info;
import com.blast.comicsisland.utils.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Collections;
import java.util.List;

/**
 * 通过详情页跳转 GridView展示章节条目
 */
public class ChapterActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener, CompoundButton.OnCheckedChangeListener {

    private TextView txtTitle;
    private TextView txtSource;
    private GridView gridView;
    private CheckBox checkBox;
    private ChapterAdapter chapterAdapter;
    private List<BookPart> bookPartList;
    private String bookId;
    private Info info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chapter);

        txtTitle = (TextView) findViewById(R.id.chapter_title_text);
        txtSource = (TextView) findViewById(R.id.chapter_book_source_name);
        gridView = (GridView) findViewById(R.id.chapter_grid_view);
        checkBox = (CheckBox) findViewById(R.id.sort_check_box);
        checkBox.setOnCheckedChangeListener(this);
        ImageView imageView = (ImageView) findViewById(R.id.chapter_back_image);
        imageView.setOnClickListener(this);

        Intent intent = getIntent();
        String sourceName = intent.getStringExtra("sourceName");
        bookId = intent.getStringExtra("bookId");
        String bigbookName = intent.getStringExtra("bigbookName");
        txtTitle.setText(bigbookName);
        txtSource.setText("来源:" + sourceName);

        if (bookId != null) {
            String path = "http://mhjk.1391.com/comic/downloadcomicsview_v3?bookid=" + bookId + "&e=" + (System.currentTimeMillis() / 1000) + "&channelid=baidu";
            ChapterAsyncTask chapterAsyncTask = new ChapterAsyncTask();
            chapterAsyncTask.execute(path);
        }
        gridView.setOnItemClickListener(this);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.chapter_back_image:
                finish();
                break;
        }
    }

    //--------------------------------->
    //gridView 点击事件
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        BookPart bookPart = bookPartList.get(position);
        String name = bookPart.getName();
        String partId = bookPart.getPartId();
        String partVersion = info.getPartVersion();
        Intent intent = new Intent(this,ContentActivity.class);
        intent.putExtra("name",name);
        intent.putExtra("partId",partId);
        intent.putExtra("partVersion",partVersion);
        intent.putExtra("bookId",bookId);

        startActivity(intent);
    }

    //--------------------------->
    //checkBox的点击事件
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            if (bookPartList != null) {
                checkBox.setText("顺序");
                Collections.reverse(bookPartList);
                chapterAdapter.notifyDataSetChanged();
            }
        } else {
            if (bookPartList != null) {
                checkBox.setText("倒序");
                Collections.reverse(bookPartList);
                chapterAdapter.notifyDataSetChanged();
            }
        }
    }

    //------------------------------->
    //异步任务
    class ChapterAsyncTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            String ret = null;
            String jsonStr = params[0];
            if (jsonStr != null) {
                String s = JsonUtils.doGetRequest(jsonStr);
                ret = JsonUtils.stringToRegex(s);
            }
            return ret;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s != null) {
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    info = new Info();
                    JSONObject infoObject = jsonObject.getJSONObject("info");
                    info.jsonParse(infoObject);
                    bookPartList = info.getBookPartList();

                    if (bookPartList != null) {
                        if (chapterAdapter == null) {
                            chapterAdapter = new ChapterAdapter(ChapterActivity.this, bookPartList);
                            gridView.setAdapter(chapterAdapter);
                        }
                        chapterAdapter.notifyDataSetChanged();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
